package com.kde.crisisdepalabras;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import com.kde.crisisdepalabras.Model.Player;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PlayerAdapter extends ArrayAdapter<Player>
{
    private Context context; 
    private int layoutResourceId;    
    private com.kde.crisisdepalabras.Model.Player data[] = null;
    public com.kde.crisisdepalabras.Model.Player pplayer = null;
    
    public PlayerAdapter(Context context, int layoutResourceId, final ArrayList<Player> players)
    {
    	super(context, layoutResourceId, players);
    	
    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = players.toArray(new Player[players.size()]);
        
        //this.setNotifyOnChange(true);
        
        /*
        for (Player player : this.data) 
        {
            new PlayerLoadPhotoUrlTask(player).execute();
            //this.
             // notifyDataSetChanged();
		} 
		*/
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	View row = convertView;
        PlayerHolder holder = null;
        
        Bitmap bitmap = null;
        URL url;

        if(convertView == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
                       
            holder = new PlayerHolder();
            holder.avatarImage = (ImageView)row.findViewById(R.id.avatarImage);
            holder.txtName = (TextView)row.findViewById(R.id.txtName);
            
            //holder.txtAddress = (TextView)row.findViewById(R.id.txtAddress);
            
            this.pplayer = data[position];
            holder.txtName.setText(pplayer.getName());
            holder.userFriendId = pplayer.getId();
            
            //holder.txtAddress.setText(player.Address);
            
    		try
    		{
    			url = new URL(this.pplayer.getImageUrl());
    			try
    			{
    				bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    				//this.Player.Image = bitmap;
    				holder.avatarImage.setImageBitmap(bitmap);
    			} 
    			catch (Exception e)
    			{
    				e.printStackTrace();
    			}
    	  	 } 
    		 catch (MalformedURLException e) 
    		 {
    			e.printStackTrace();
    		 }
            
             row.setTag(holder);

        	LayoutInflater inflater1 = ((Activity)context).getLayoutInflater();
	        convertView = inflater1.inflate(R.layout.player, null);
	        holder = new PlayerHolder();
	        holder.avatarImage = (ImageView)convertView.findViewById(R.id.avatarImage);
	        holder.txtName = (TextView)convertView.findViewById(R.id.txtName);
	        convertView.setTag(holder);

        }

        {
        	holder = (PlayerHolder)row.getTag();
        }
       
        Player player = data[position];
        holder.txtName.setText(player.getName());  
        holder.userFriendId = player.getId();

        
        holder = (PlayerHolder)convertView.getTag();
        holder.imageURL = data[position].getImageUrl();
        holder.txtName.setText(data[position].getName());
        
        new DownloadPhotoTask().execute(holder);
       
        return convertView;
    }
    
    static class PlayerHolder
    {
    	String imageURL;
    	Bitmap bitmap;
        ImageView avatarImage;
        TextView txtName;
        String userFriendId;
        //TextView txtAddress;

    }
}